package com.mnt.gui.base.logger;

import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.util.Duration;

import org.controlsfx.control.Notifications;

/**
 * display all message with pop by the mian view
 * @author 2014-7-3 mnt.cico
 *
 */
public class TipDialogLogger {

	protected TipDialogLogger() {
		// empty
	}

	private static Stage stage;
	private static Pos defaultPos = Pos.TOP_RIGHT;
	
	private static boolean isStage;

	public final static void init(Stage stage) {
		TipDialogLogger.stage = stage;
	}

	public final static void IsUseSatge(boolean isStage) {
		TipDialogLogger.isStage = isStage;
	}
	
	public final static void setDirection(Pos pos) {
		defaultPos = pos;
	}

	public static void error(String message) {
		error(stage, message);
	}

	public static void debug(String message) {
		debug(stage, message);
	}

	public static void warning(String message) {
		warning(stage, message);
	}

	public static void error(Exception exception) {
		error(stage, exception);
	}
	
	public static void successed(String message) {
		successed(stage, message);
		
	}

	public static void error(Stage stage, String message) {
		checkInit(stage);
		showNotification(stage, "错误！", message, TipType.ERROR);
	}
	
	public static void successed(Stage stage, String message) {
		checkInit(stage);
		showNotification(stage, "成功！", message, TipType.SUCCESED);
	}

	public static void debug(Stage stage, String message) {
		checkInit(stage);
		showNotification(stage, "调试！", message, TipType.CONFIRM);
	}

	public static void warning(Stage stage, String message) {
		checkInit(stage);
		showNotification(stage, "警告！", message, TipType.WARNING);
	}

	public static void error(Stage stage, Exception exception) {
		checkInit(stage);
		showNotification(stage, "错误！", exception.getMessage(), TipType.ERROR);
	}

	/**
	 * check is init stage 2014-7-3 mnt.cico
	 * 
	 * @param stage
	 */
	private static void checkInit(Stage stage) {
		if (null == stage) {
			throw new NullPointerException("you need init TipDialogLogger");
		}
	}
	
	private static void showNotification(Stage stage, String title, String context, TipType tipType) {
		Notifications notification = Notifications.create().title(title).text(context).hideAfter(Duration.seconds(3d)).position(defaultPos).hideCloseButton();
		if(isStage) {
			notification.owner(stage);
		}
		
		switch(tipType) {
		case ERROR:
			notification.showError();
			break;
		case SUCCESED:
			notification.showInformation();
			break;
		case WARNING:
			notification.showWarning();
			break;
		case CONFIRM:
			notification.showConfirm();
			break;
		}
	}

//	/**
//	 * get the tip dialog bounds
//	 * 2014-7-3 mnt.cico
//	 * @return Pair<Double, Double> key is layoutX value is layoutY
//	 */
//	private static Pair<Double, Double> getShowBounds() {
//		Pair<Double, Double> result = null;
//		switch (defaultPos) {
//		case BASELINE_CENTER:
//			result = new Pair<Double, Double>(1d, 1d);
//			break;
//		case BASELINE_LEFT:
//			break;
//		case BASELINE_RIGHT:
//			break;
//		case BOTTOM_CENTER:
//			break;
//		case BOTTOM_LEFT:
//			break;
//		case BOTTOM_RIGHT:
//			break;
//		case CENTER:
//			break;
//		case CENTER_LEFT:
//			break;
//		case CENTER_RIGHT:
//			break;
//		case TOP_CENTER:
//			break;
//		case TOP_LEFT:
//			break;
//		case TOP_RIGHT:
//			break;
//		}
//
//		return result;
//	}
	
	public enum TipType {
		ERROR, SUCCESED, WARNING, CONFIRM;
	}
}
